
import json
import scrapy
import time

from ..items import VideobarrageItem

class DemoSpider(scrapy.Spider):
    name = 'demo'
    allowed_domains = ['bilibili.com']
    start_urls = ['http://comment.bilibili.com/519456863.xml']

    def parse(self, response):
        items = VideobarrageItem()
        danmus = response.xpath('//d')
        for danmu in danmus:
            info = danmu.xpath('./@p').extract_first().split(',')
            info[4] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(info[4])))
            # 获取弹幕内容
            content = danmu.xpath('./text()').extract_first()
            items['bmode'] = info[1]
            items['sendtime'] = info[4]
            items['userid'] = info[6]
            items['bvid'] = 'BV1YS4y167nw'
            items['content'] = content
            # print(items)
            yield items
        # for danmu in danmus:
        #     # # 获取d元素的p属性值
        #     # things = danmu.xpath('./@p')[0].split(',')
        #     # 获取弹幕内容 并去掉所有空格
        #     content = danmu.xpath('./text()')[0].replace(" ", "")
        #     item = {'内容': content}
        #     print(item)

